<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <!-- 引入axios -->
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
    <!-- 引入Vue -->
    <script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
    <!-- import Element UI -->
    <script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
    <link rel="stylesheet" href="css/index.css">
</head>
<body>
<div id="app">
    <el-container>
        <el-header>
            <h1>
                <img src="../imgs/shark.png">
                CoolShark商城管理系统
                <span v-if="" style="float: right;font-size: 15px;">
                    <a href="JavaScript:void(0)" style="text-decoration: none ;font-family: 楷体"
                       @click="logout()">退出登录</a>
                </span>
            </h1>
        </el-header>
        <el-container>
            <el-aside>
                <!-- :unique-opened="true":设置子菜单只允许打开一个--->
                <el-menu
                        default-active="6"
                        class="el-menu-vertical-demo"
                        :unique-opened="true"
                        @select="handleSelect"
                        @open="handleOpen"
                        @close="handleClose">
                    <el-menu-item index="1">
                        <i class="el-icon-s-home"></i>
                        <span slot="title">首页</span>
                    </el-menu-item>
                    <el-submenu index="2">
                        <template slot="title">
                            <i class="el-icon-user-solid"></i>
                            <span>管理员管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="2-1" v-if="username==='root'">添加管理员</el-menu-item>
                            <el-menu-item index="2-2">管理员列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-submenu index="3">
                        <template slot="title">
                            <i class="el-icon-s-claim"></i>
                            <span>类别管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="3-1">添加类别</el-menu-item>
                            <el-menu-item index="3-2">类别列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-submenu index="4">
                        <template slot="title">
                            <i class="el-icon-s-order"></i>
                            <span>商品管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="4-1">添加商品</el-menu-item>
                            <el-menu-item index="4-2">商品列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-submenu index="5">
                        <template slot="title">
                            <i class="el-icon-picture"></i>
                            <span>轮播图管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="5-1">添加轮播图</el-menu-item>
                            <el-menu-item index="5-2">轮播图列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-menu-item index="6">
                        <i class = "el-icon-user-solid"></i>
                        <span>个人信息</span>
                    </el-menu-item>
                </el-menu>
            </el-aside>
            <el-main>
                <el-card style="width: 500px; margin: 0 auto">
                    <h1 style="color: #000;margin-left: 200px">修改个人信息</h1>
                    <el-form label-width="80px" :model="admin" :rules="rules" ref="admin">
                        <el-form-item label="用户名">
                            <el-input v-model="admin.username" disabled></el-input>
                        </el-form-item>
                        <el-form-item label="原始密码" prop="oldPassword">
                            <el-input v-model="admin.oldPassword"></el-input>
                        </el-form-item>
                        <el-form-item label="密码" prop="password">
                            <el-input v-model="admin.password" show-password></el-input>
                        </el-form-item>
                        <el-form-item label="确认密码" prop="checkPassword">
                            <el-input v-model="admin.checkPassword" show-password></el-input>
                        </el-form-item>
                        <el-form-item label="邮箱" prop="email">
                            <el-input v-model="admin.email"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" style="margin-left: 125px" @click="updateAdmin">修改</el-button>
                        </el-form-item>
                    </el-form>
                </el-card>
            </el-main>
        </el-container>
    </el-container>
</div>
</body>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                username: localStorage.getItem('username'),
                goodsNum: '',
                userNum: '',
                admin: {
                    username: '',
                    oldPassword: '',
                    password: '',
                    checkPassword: '',
                    email: ''
                },
                rules: {
                    oldPassword: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'}],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'}],
                    checkPassword: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'},
                        // 对两次的密码进行校验,判断是否相同
                        {
                            validator: (rule, value, callback) => {
                                if (value !== v.user.password) {
                                    callback(new Error('两次密码不一致'))
                                }
                            }
                        }],
                    email: [
                        {required: true, message: '请输入邮箱地址', trigger: 'blur'},
                        {
                            pattern: /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/,
                            message: '请输入正确的邮箱地址',
                            trigger: 'blur'
                        }
                    ]
                }
            }
        },
        methods: {
            /**
             * 根据不同的key值，重定向到相应的页面。
             * key: 用于识别要跳转到哪个页面的标识符。
             * 无返回值。
             */
            handleSelect(key, keyPath) {
                if (key === '2-1') {
                    location.href = 'AddAdmin.html'; // 跳转到添加管理员页面
                } else if (key === '2-2') {
                    location.href = 'AdminList.html'; // 跳转到管理员列表页面
                } else if (key === '3-1') {
                    let name = prompt("请输入类别名称：", "");
                    if (name === null) {
                        return;
                    }
                    if (name.trim() === '') {
                        v.$message({
                            message: '类别名称不能为空！',
                            type: 'warning'
                        });
                    }
                    axios.get('/admin/insertCarousel?name=' + name)
                        .then(function (response) {
                            v.carousel = response.data.data;
                            location.href = 'CarouselList.html'
                        });
                } else if (key === '3-2') {
                    location.href = 'CarouselList.html'; // 跳转到类别列表页面
                } else if (key === '4-1') {
                    location.href = 'AddGoods.html'
                } else if (key === '4-2') {
                    location.href = 'GoodsList.html'
                } else if (key === '5-1') {
                    location.href = 'AddBanner.html'
                } else if (key === '5-2') {
                    location.href = 'BannerList.html'
                }
            },
            handleOpen(key, keyPath) {
                console.log('key = ' + key, 'leyPath = ' + keyPath);
            },
            handleClose(key, keyPath) {
                console.log('key = ' + key, 'leyPath = ' + keyPath);
            },
            logout() {
                localStorage.removeItem('username');
                location.href = '../login.html';
            },
            updateAdmin() {
                axios.post('/admin/updateAdmin', v.admin)
                    .then(function (response) {
                        let responseBody = response.data;
                        if (responseBody.state === 20000) {
                            v.$message({
                                message: '修改成功！',
                                type: 'success'
                            });
                        }else if (responseBody.state === 40000) {
                            v.$message.error(responseBody.message);
                        }
                    })
            }
        },
        created: function () {
            axios.get('/admin/getAdminByUsername', {
                params: {
                    username: localStorage.getItem('username')
                }
            })
                .then(function (response) {
                    let responseBody = response.data;
                    if (responseBody.state === 20000) {
                        v.admin.username = responseBody.data.username;
                        v.admin.email = responseBody.data.email;
                    }
                });
        }
    })
</script>
</html>